﻿#region
using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevTools.Properties;

#endregion

namespace DevTools.AndroidLayoutHelper
{
    public partial class AxmlListControl : XtraUserControl
    {
        public AxmlListControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            if (string.IsNullOrEmpty(Settings.Default.AndroidLayoutHelper_AxmlFolder) == false && Directory.Exists(Settings.Default.AndroidLayoutHelper_AxmlFolder))
                btnRefresh.PerformClick();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            lbcList.Items.Clear();

            var files = Directory.GetFiles(Settings.Default.AndroidLayoutHelper_AxmlFolder, "*.axml");
            foreach (var file in files)
                lbcList.Items.Add(Path.GetFileName(file));
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbdBrowse.ShowDialog() != DialogResult.OK)
                return;

            Settings.Default.AndroidLayoutHelper_AxmlFolder = fbdBrowse.SelectedPath;
            btnRefresh.PerformClick();
        }

        private void lbcList_DoubleClick(object sender, EventArgs e)
        {
            OnLayoutSelected((string)lbcList.SelectedItem);
        }

        #region LayoutSelected event things for C# 3.0
        public event EventHandler<LayoutSelectedEventArgs> LayoutSelected;

        protected virtual void OnLayoutSelected(LayoutSelectedEventArgs e)
        {
            if (LayoutSelected != null)
                LayoutSelected(this, e);
        }

        protected virtual void OnLayoutSelected(string fileName)
        {
            OnLayoutSelected(new LayoutSelectedEventArgs(fileName));
        }

        protected virtual LayoutSelectedEventArgs OnLayoutSelectedWithReturn(string fileName)
        {
            LayoutSelectedEventArgs args = new LayoutSelectedEventArgs(fileName);
            OnLayoutSelected(args);

            return args;
        }

        public class LayoutSelectedEventArgs : EventArgs
        {
            public string FileName { get; set; }

            public LayoutSelectedEventArgs()
            {
            }

            public LayoutSelectedEventArgs(string fileName)
            {
                FileName = fileName;
            }
        }
        #endregion
    }
}